<template>
  <div class="weekly-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>周报管理</span>
          <el-button type="primary" @click="showAddWeeklyForm">新增周报</el-button>
        </div>
      </template>

      <!-- 搜索框 -->
      <div class="search-section">
        <el-form :inline="true" class="search-form">
          <el-form-item label="周报标题">
            <el-input v-model="searchTitle" placeholder="请输入周报标题" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchByTitle">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 周报列表 -->
      <el-table :data="weeklys" style="width: 100%" border stripe>
        <el-table-column prop="reportID" label="周报ID" width="100" />
        <el-table-column prop="username" label="作者用户名" width="120" />
        <el-table-column prop="reportTitle" label="周报标题" width="200" />
        <el-table-column prop="reportContent" label="周报内容" show-overflow-tooltip />
        <el-table-column prop="submitTime" label="提交时间" width="180" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click="editWeekly(scope.row)">编辑</el-button>
            <el-button type="danger" link @click="deleteWeekly(scope.row.reportID)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 表单弹窗 -->
    <el-dialog
      :title="selectedWeekly ? '编辑周报' : '新增周报'"
      v-model="showForm"
      width="50%"
      :close-on-click-modal="false"
    >
      <WeeklyForm
        :weekly="selectedWeekly"
        @close="closeForm"
        @save="fetchReports"
      />
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import WeeklyForm from "./WeeklyForm.vue";

export default {
  name: "WeeklyList",
  components: { WeeklyForm },
  data() {
    return {
      weeklys: [], // 周报列表
      showForm: false, // 控制表单弹窗显示
      selectedWeekly: null, // 当前选中的周报
      searchTitle: "", // 搜索框输入内容
    };
  },
  methods: {
    // 获取周报列表
    fetchReports() {
      axios
        .get("/weekly/list")
        .then((response) => {
          this.weeklys = response.data;
        })
        .catch((error) => {
          console.error("获取周报列表失败:", error);
          alert("获取周报列表失败，请稍后再试！");
        });
    },

    // 根据标题查询周报
    searchByTitle() {
      if (!this.searchTitle.trim()) {
        alert("请输入周报标题！");
        return;
      }
      axios
        .get("/weekly/searchByTitle", {
          params: { reportTitle: this.searchTitle },
        })
        .then((response) => {
          this.weeklys = response.data;
        })
        .catch((error) => {
          if (error.response && error.response.status === 404) {
            alert("未找到匹配的周报！");
          } else {
            console.error("查询周报失败:", error);
            alert("查询失败，请稍后再试！");
          }
        });
    },

    // 重置搜索框
    resetSearch() {
      this.searchTitle = "";
      this.fetchReports(); // 重置后重新获取全部周报
    },

    // 显示新增周报表单
    showAddWeeklyForm() {
      this.selectedWeekly = null;
      this.showForm = true;
    },

    // 编辑周报
    editWeekly(weekly) {
      this.selectedWeekly = { ...weekly }; // 克隆对象，防止直接修改
      this.showForm = true;
    },

    // 删除周报
    deleteWeekly(reportID) {
      if (confirm("确定要删除此周报吗？")) {
        axios
          .delete(`/weekly/delete?reportID=${reportID}`)
          .then(() => {
            alert("删除成功！");
            this.fetchReports();
          })
          .catch((error) => {
            console.error("删除周报失败:", error);
            alert("删除失败，请稍后再试！");
          });
      }
    },

    // 关闭表单
    closeForm() {
      this.showForm = false;
    },
  },
  mounted() {
    this.fetchReports();
  },
};
</script>

<style scoped>
.weekly-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-section {
  margin-bottom: 20px;
  padding: 18px 0;
}

.search-form {
  display: flex;
  align-items: center;
}
</style>
  